package edu.wong.controller;

import edu.wong.service.FlowLimitService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class FlowLimitController {


    @GetMapping("/testA")
    public String testA(){
        return "------testA";
    }
    @GetMapping("/testB")
    public String testB(){
        return "------testB";
    }


    @Resource
    private FlowLimitService flowLimitService;

    /**
     * 链路演示
     * C和D两个请求都访问flowLimitService.common()方法，阈值达到后对C限流，对D不限
     */
    @GetMapping("/testC")
    public String testC(){
        flowLimitService.common();
        return "------testC";
    }
    @GetMapping("/testD")
    public String testD(){
        flowLimitService.common();
        return "------testD";
    }

    @GetMapping("/testE")
    public String testE(){
        System.out.println(System.currentTimeMillis()+"     testE,排队等候");
        return "-----testE";
    }
    @GetMapping("/testF")
    public String testF(){
        try{
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("-----测试；新增熔断规则-慢调用比例");
        return "-------testF 新增熔断规则-慢调用比例";
    }

    @GetMapping("/testG")
    public String testG(){
        System.out.println("----测试：新增熔断规则-异常比例");
        int age=10/0;
        return "-----testG,：新增熔断规则-异常比例";
    }
    @GetMapping("/testH")
    public String testH(){
        System.out.println("----测试：新增熔断规则-异常数");
        int age=10/0;
        return "-----testH,：新增熔断规则-异常数";
    }
}
